import { hashCode } from './util'

// 列表转换树
export function getTree(data, pushFunc = (item, parent, children) => {
    children.push(item);
}, idKey = 'id', parentIdKey = 'parentId', sortKeys = ['orderNum']) {
    let sortFunc = (a1, a2) => {
        let result = 0;
        for (let index in sortKeys) {
            let key = sortKeys[index]
            try {
                result += a1[key] - a2[key];
            } catch (e) {
                result += hashCode(a1[key] + "") - hashCode(a2[key] + "");
            }
        }
        return result;
    };
    let newData = JSON.parse(JSON.stringify(data))
    // // 删除 所有 children,以防止多次调用
    // data.forEach(function (item) {
    //     delete item.children;
    // });
    // 将数据存储为 以 id 为 KEY 的 map 索引数据列
    var map = {};
    newData.forEach(function (item) {
        map[item[idKey]] = item;
    });
    var val = [];
    newData.forEach(function (item) {
        // 以当前遍历项，的pid,去map对象中找到索引的id
        var parent = map[item[parentIdKey]];
        // 好绕啊，如果找到索引，那么说明此项不在顶级当中,那么需要把此项添加到，他对应的父级中
        if (parent) {
            let children = (parent.children || ( parent.children = [] ));
            pushFunc(item, parent, children);
            children.sort(sortFunc);
        } else {
            //如果没有在map中找到对应的索引ID,那么直接把 当前的item添加到 val结果集中，作为顶级
            pushFunc(item, null, val);
        }
    });
    val.sort(sortFunc);
    return val;
}

// 根据属性递归查找所在节点
export function recursion(data, current, resultKey='path'){
    var result = null;
    if(!data){
        // return; 中断执行
        return;
    }
    for(var i in data){
        if (result != null) {
            break;
        }
        var item = data[i];
        if(item[resultKey] === current){
            result = item;
            break;
        }else if(item.children && item.children.length > 0){
            result = recursion(item.children, current);
        }
    }
    return result;
}

// 获取所有上级节点
export function parents(data, id, parentIdKey = 'parentId' ) {
    var parentNodes = [];//所有父节点
    var forfun = function (id,nodes) {
        for(var i=0;i<nodes.length;i++){
            var currentNode = nodes[i];
            if(currentNode.id === id){
                return currentNode;
            }else if(currentNode.children){
                var validNodeId = forfun(id,currentNode.children);
                if(validNodeId&&parentNodes.indexOf(validNodeId)<0){
                    parentNodes.push(validNodeId)
                }
                if(validNodeId){
                    return currentNode;
                }
            }
        }
    }
    var validNodeId = forfun(id,data);
    if(validNodeId&&parentNodes.indexOf(validNodeId)<0){
        parentNodes.push(validNodeId);
    }
    return parentNodes;
}

// 获取所有下级节点
export function childs(list, id) {
    let result = []
    let tempMap = {}
    list.forEach(function (item) {
        tempMap[item.id] = item;
    });
    var findChilds = function(id) {
        list.forEach((d) => {
            if (d.parentId === id) {
                findChilds(d.id)
            } else if (d.id === id) {
                result.push(tempMap[id])
            }
        })
    }
    findChilds(id)
    return result
}
